from xml.sax import make_parser
from xml.sax.handler import ContentHandler

class SaxHandler(ContentHandler):
        """ Callback class for SAX parser """
	filelist = {}
        chars = ""
	current_directory = None
	def __init__(self):
		self.filelist = {}
		self.chars = ""
		self.current_element = None
		self.current_directory = list()

        def startElement(self,name,attrs):
                """ Handle for element begin """
		if name == 'Directory':
			dirname = attrs.get('Name')
			self.current_directory.append(dirname)
		elif name == 'File':
			filename = attrs.get('Name').replace("&amp;","&")
			filesize = attrs.get('Size')
			filetth = attrs.get('TTH')
			prefix = ""
			for i in self.current_directory:
				prefix = prefix + '/' + i
			self.filelist[prefix + '/' + filename] = (filesize,filetth)

        def endElement(self,name):
                """ Handle for finished element """
		if name == 'Directory':
			self.current_directory.pop()

        def characters (self,ch):
                """ Handle for character data """
                sch = ch.lstrip()
                if not sch == "":
                        self.chars = sch

